<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:schema_1_0="http://schemas.microsoft.com/ado/2006/04/edm"
    xmlns:schema_1_1="http://schemas.microsoft.com/ado/2007/05/edm"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

<xsl:output method="text"/>

<!-- Default service URI passed externally -->  
<xsl:param name="DefaultServiceURI"/>

<xsl:template match="/">&lt;?php
/**
 *
 * Copyright (c) 2009, Persistent Systems Limited
 *
 * Redistribution and use, with or without modification, are permitted
 *  provided that the following  conditions are met:
 *   - Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   - Neither the name of Persistent Systems Limited nor the names of its contributors
 *     may be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * This code was generated by the tool 'PHPDataSvcUtil.php'.
 * Runtime Version:1.0
 *
 * Changes to this file may cause incorrect behavior and will be lost if
 * the code is regenerated.
 */

// Get ADO .NET Data Services PHP Toolkit library path
$library_path = get_cfg_var('adodotnetservicesphp_path');
if (strlen($library_path) == 0)
{
    throw new Exception("The configuration option 'adodotnetservicesphp_path' is not set in the php.ini file, "
                        . "Please refer installation instructions for fix this issue");
}

// Refer phpdataservices framework classes
require_once 'Resource/Messages.php';
require_once 'Common/Dictionary.php';
require_once 'Context/EntityStates.php';
require_once 'Interfaces/Entity.php';
require_once 'Context/ResourceBox.php';
require_once 'Context/RelatedEnd.php';
require_once 'Context/SaveResult.php';
require_once 'Common/Guid.php';
require_once 'Credential/Credential.php';
require_once 'Common/Utility.php';
require_once 'Context/QueryProcessor.php';
require_once 'Context/ObjectContext.php';
require_once 'Parser/Parser.php';
require_once 'Parser/AtomParser.php';
require_once 'Parser/JSONParser.php';
require_once 'Interfaces/Object.php';
require_once 'WebUtil/HttpBatchRequest.php';
require_once 'WebUtil/HttpBatchResponse.php';
require_once 'WebUtil/HttpRequest.php';
require_once 'WebUtil/Microsoft_Http_Response.php';
require_once 'WebUtil/HttpResponse.php';
require_once 'HttpProxy.php';
require_once 'ADODotNETDataServicesException.php';

/**
 * Defines default ADO .NET Data Service URL for this proxy class
 */
define("DEFAULT_ADO_DOT_NET_SERVICE_URL", "<xsl:value-of select="$DefaultServiceURI"/>");
<xsl:apply-templates select="/edmx:Edmx/edmx:DataServices/schema_1_0:Schema | /edmx:Edmx/edmx:DataServices/schema_1_1:Schema"/>
?&gt;</xsl:template>

<xsl:template match="/edmx:Edmx/edmx:DataServices/schema_1_0:Schema | /edmx:Edmx/edmx:DataServices/schema_1_1:Schema">
  <xsl:apply-templates select="schema_1_0:EntityContainer | schema_1_1:EntityContainer"/>
<xsl:for-each select="schema_1_0:EntityType | schema_1_1:EntityType">
    <xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>

<!-- Generate container class -->
<xsl:template match="schema_1_0:EntityContainer | schema_1_1:EntityContainer">
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="service_namespace" select="concat(//schema_1_0:EntityType/../@Namespace, //schema_1_1:EntityType/../@Namespace)" />  
/**
 * Container class <xsl:value-of select="@Name"/>, Namespace: <xsl:value-of select="$service_namespace"/>
 */
class <xsl:value-of select="@Name"/> extends ObjectContext
{
    /**
     * The constructor for <xsl:value-of select="@Name"/> accepting service URI
     */
    public function __construct($uri = "")
    {
        if (strlen($uri) == 0)
        {
            $uri = DEFAULT_ADO_DOT_NET_SERVICE_URL;
        }
        
        if (Utility::reverseFind($uri, '/') != strlen($uri) - 1) 
        {
           $uri = $uri . '/';
        }

        $this->_baseURI = $uri;
        parent::ObjectContext($this->_baseURI);
        $this->_entities = array(<xsl:for-each select="schema_1_0:EntitySet | schema_1_1:EntitySet">"<xsl:value-of select="@Name"/>"<xsl:if test="position() != last()">, <xsl:text>&#10;                                 </xsl:text>
</xsl:if></xsl:for-each>);
        $this->_entitySet2Type = array(<xsl:for-each select="schema_1_0:EntitySet | schema_1_1:EntitySet">"<xsl:value-of select="translate(@Name, $uppercase, $smallcase)" />" =&gt; "<xsl:value-of select="substring-after(@EntityType, concat($service_namespace, '.'))"/>"<xsl:if test="position() != last()">, <xsl:text>&#10;                                 </xsl:text>
</xsl:if></xsl:for-each>);
        $this->_entityType2Set = array(<xsl:for-each select="schema_1_0:EntitySet | schema_1_1:EntitySet">"<xsl:value-of select="translate(substring-after(@EntityType, concat($service_namespace, '.')), $uppercase, $smallcase)" />" =&gt; "<xsl:value-of select="@Name"/>"<xsl:if test="position() != last()">, <xsl:text>&#10;                                 </xsl:text>
</xsl:if></xsl:for-each>);
    }

    /**
     * Functions for adding object to the entityset/collection
     */
    <xsl:for-each select="schema_1_0:EntitySet | schema_1_1:EntitySet">
    /**
     * Add <xsl:value-of select="@Name"/>
     */
    public function AddTo<xsl:value-of select="@Name"/>($object)
    {
        return parent::AddObject('<xsl:value-of select="@Name"/>', $object);
    }
    </xsl:for-each>

    /**
     * This function returns the entities.
     */
    public function getEntities()
    {
        return $this->_entities;
    }
}
</xsl:template>

<xsl:template match="schema_1_0:EntityType |schema_1_1:EntityType">
/**
 * The <xsl:value-of select="@Name"/> class
 */
class <xsl:value-of select="@Name"/> extends Object
{
    protected $_entityMap = array();
    protected $_entityKey = array();
    protected $_relLinks  = array();
    protected $_baseURI;
    <xsl:for-each select="schema_1_0:Property | schema_1_1:Property">
    /**
     * EntityProperty<xsl:if test="@Nullable = 'false'">
     * NotNullable</xsl:if>
     * <xsl:value-of select="@Type"/>
     */
    public $<xsl:value-of select="@Name"/>;</xsl:for-each>
    <xsl:for-each select="schema_1_0:NavigationProperty | schema_1_1:NavigationProperty">
    /**
     * NavigationProperty
     */
    public $<xsl:value-of select="@Name"/>;</xsl:for-each>

    /**
     * Function to create an instance of <xsl:value-of select="@Name"/>
     */
    public static function Create<xsl:value-of select="@Name"/>(<xsl:for-each select="schema_1_0:Property[@Nullable = 'false'] | schema_1_1:Property[@Nullable = 'false']">$<xsl:value-of select="@Name"/><xsl:if test="position() != last()">, </xsl:if></xsl:for-each>)
    {   <xsl:variable name="ClassName" select="@Name"/>
        $<xsl:value-of select="@Name"/> = new <xsl:value-of select="@Name"/>();<xsl:for-each select="schema_1_0:Property[@Nullable = 'false'] | schema_1_1:Property[@Nullable = 'false']">
        $<xsl:value-of select="$ClassName"/>-><xsl:value-of select="@Name"/> = $<xsl:value-of select="@Name"/>;</xsl:for-each>
        return $<xsl:value-of select="@Name"/>;
    }

    /**
     * Constructor for <xsl:value-of select="@Name"/>
     */
    public function __construct($uri = "")
    {
        $this->_objectID = Guid::NewGuid();        
        $this->_baseURI = $uri;
        <xsl:for-each select="schema_1_0:NavigationProperty | schema_1_1:NavigationProperty">
        $this->_entityMap['<xsl:value-of select="@Name"/>'] = '<xsl:value-of select="@ToRole"/>';
        </xsl:for-each>
        <xsl:apply-templates select="schema_1_0:Key | schema_1_1:Key"/>
    }

    /**
     * overring getObjectID() functon of Object class
     */
    public function getObjectID()
    {
        return $this->_objectID;
    }

    /**
     * This function returns the entity keys of this entity.
     */
    public function getEntityKeys()
    {
        return $this->_entityKey;
    }

    /**
     * This function set the entity keys of this entity.
     */
    public function setEntityKeys($entityKey)
    {
        $this->_entityKey = $entityKey;
    }

    /**
     * This function returns the related links of this entity.
     */
    public function getRelatedLinks()
    {
        return $this->_relLinks;
    }

    /**
     * This function set the related links of this entity.
     */
    public function setRelatedLinks($relLinks)
    {
        $this->_relLinks = $relLinks;
    }

    /**
     * Function for getting Entity Type Name corrosponding to navigation Name
     */
    public function getActualEntityTypeName($key)
    {
        if (array_key_exists  ($key, $this->_entityMap))
        {
            return ($this->_entityMap[$key]);
        }

        return $key;
    }
}
</xsl:template>

<xsl:template match="schema_1_0:Key | schema_1_1:Key">
    <xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef">
        $this->_entityKey[] = '<xsl:value-of select="@Name"/>';
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
